package com.qiu.wbpostman.service;

import java.util.Date;

import com.qiu.wbpostman.common.WeiBoTool;
import com.qiu.wbpostman.dao.UserMailDao;
import com.qiu.wbpostman.dao.UserTokenDao;
import com.qiu.wbpostman.entity.UserMail;
import com.qiu.wbpostman.entity.UserToken;

import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.WeiboException;
import weibo4j.http.AccessToken;

public class WbService {
	UserTokenDao userTokenDao = new UserTokenDao(UserToken.class);
	UserMailDao usermailDao = new UserMailDao(UserMail.class);

	public void saveAUserToken(AccessToken accessToken) {
		UserToken userToken = new UserToken();
		userToken.setId(Long.parseLong(accessToken.getUserId() + ""));
		userToken.setToken(accessToken.getToken());
		userToken.setTokenSecret(accessToken.getTokenSecret());
		userToken.setCreateDate(new Date());
		userTokenDao.creat(userToken);
	}

	public boolean checkUserMailIsExist(Long id) {
		UserMail userMail = usermailDao.getUserMailById(id);
		if (userMail.getId() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public User getCurrentUser(AccessToken accessToken) throws WeiboException {
		Weibo weibo = WeiBoTool.getWeibo(true, accessToken, "", "");
		User user = weibo.getUserDetail(accessToken.getUserId() + "");
		return user;
	}

	public void saveUserMail(UserMail userMail) {

		usermailDao.creat(userMail);
	}
}
